package com.anhry.app.safety.manage.tjfx.struts.base;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

public class ExportExcel {

	private List dataList;

	private List titleList;

	private String fileName;

	public ExportExcel() {
	}

	public ExportExcel(List dataList, List titleList, String fileName) {
		this.dataList = dataList;
		this.titleList = titleList;
		this.fileName = fileName;
	}
	
	public static void main(String[] args) {
		List<Object[]> list1 = new ArrayList<Object[]>();
		List<String> list2 = new ArrayList<String>();
		Object[] data1 = new Object[]{"我的","2","3","4"};
		Object[] data2 = new Object[]{"11","21","31","41"};
		list1.add(data1);
		list1.add(data2);
		list2.add("1");
		list2.add("2");
		list2.add("3");
		list2.add("4");
		ExportExcel e = new ExportExcel(list1,list2,"test");
		e.createWorkbook();
	}

	public HSSFWorkbook createWorkbook() {
		try {
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet(fileName);
			createRow(sheet, titleList,workbook);
			createData(sheet,workbook);
			return workbook;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}

	/**
	 * 生成标题行和表头行
	 * 
	 * @param workBook
	 * @param titleList
	 * @return
	 */
	public void createRow(HSSFSheet sheet, List titleList, HSSFWorkbook workbook) {
		// 标题行 合并居中
		HSSFCellStyle style = workbook.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
//		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//左右居中
//		//字体样式
//		HSSFFont titleFont = workbook.createFont();
//        titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//        //设置字体大小
//        titleFont.setFontHeightInPoints((short)16);
//        //设置颜色
//        titleFont.setColor(HSSFColor.DARK_BLUE.index);
//        style.setFont(titleFont);
        
		sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, titleList.size()-1));
		HSSFRow firstRow = sheet.createRow(0);
		HSSFCell cellT = firstRow.createCell(0);
		cellT.setCellStyle(style);
		cellT.setCellValue(fileName);
		// 表头行
		HSSFRow row = sheet.createRow(1);
		for (int i = 0; i < titleList.size(); i++) {
			HSSFCell cell = row.createCell(i);
			HSSFRichTextString text = new HSSFRichTextString(titleList.get(i).toString());
			cell.setCellValue(text);
			cell.setCellStyle(style);
		}
	}

	/**
	 * 遍历集合数据，产生数据行
	 * @param sheet
	 * @param workbook
	 */
	public void createData(HSSFSheet sheet,HSSFWorkbook workbook) {
		int index = 2;//行下标
		HSSFRow row;
		HSSFCell cell;
		HSSFCellStyle style = workbook.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		for(int i=0;i<dataList.size();i++){
			Object dataRow[] = (Object[])dataList.get(i);
			row = sheet.createRow(index);
			index++;
			for(int j=0;j<dataRow.length;j++){
				cell = row.createCell(j);
				cell.setCellValue(dataRow[j].toString());
				cell.setCellStyle(style);
			}
		}
	}
}
